package com.example.yijian.edit.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yijian.R;
import com.example.yijian.edit.adapter.MaskAdapter;
import com.example.yijian.edit.data.mask.MaskBean;
import com.example.yijian.edit.mask.MaskUtils;

public class MaskControlView extends RelativeLayout {
    private Context mContext;
    private View rootView;
    private RecyclerView maskRecyclerView;
    private TextView tvReverse;
    private ImageView ivFinish;
    private MaskAdapter menuAdapter;

    public MaskControlView(Context context) {
        super(context);
        mContext = context;
        this.init();
    }

    public MaskControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.init();
    }

    public MaskControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        this.init();
    }

    private void init() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.view_mask, this);
        initView();
        rootView.setOnClickListener(v -> {
        });
        tvReverse.setOnClickListener(v -> {
            if (onFunctionListener != null) {
                onFunctionListener.onMaskReserve();
            }
        });
        ivFinish.setOnClickListener(v -> {
            if (onFunctionListener != null) {
                onFunctionListener.onMaskConfirm();
            }
        });
    }

    private void initView() {
        maskRecyclerView = findViewById(R.id.maskTypeRv);
        tvReverse = findViewById(R.id.tv_mask_reverse);
        ivFinish = findViewById(R.id.iv_mask_finish);
        menuAdapter = new MaskAdapter(MaskUtils.getMaskMenus(getContext()), getContext());
        maskRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        maskRecyclerView.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(maskBean -> {
            if (maskBean != null) {
                if (onFunctionListener != null) {
                    onFunctionListener.onMaskSelected(maskBean);
                }
            }
            menuAdapter.notifyDataSetChanged();
        });
    }


    private OnFunctionListener onFunctionListener;

    public void setOnFunctionListener(OnFunctionListener callBack) {
        this.onFunctionListener = callBack;
    }

    public interface OnFunctionListener {
        void onMaskSelected(MaskBean maskBean);

        void onMaskReserve();

        void onMaskConfirm();
    }
}
